/**
 * Created by zhanglihe on 15/11/7.
 */
require.config(require_config);

define([
    'jquery',
    'common',
    'ready',
    'collection',
    'template',
    'model',
    'dialog',
    'validation'
], function( $, common, ready, Collection, template, Model, dialog, Validation ) {
    var index = function() {
        this.init();
    };
    index.prototype = {
        init: function() {
            loading();
            this.fetch();
            this.event();
        },

        fetch: function() {
            var _this = this;
            this.collection = new Collection({
                url: _global.url.api + 'partners',
                model: {
                    url: {
                        put: _global.url.api + 'partner/{id}'
                    },
                    event: {
                        '.item-edit': 'edit',
                        '.item-reset-pwd': 'resetPwd'
                    },
                    edit: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            title : '修改',
                            content: template('edit-tpl', this.data),
                            width: 600,
                            ok: function() {
                                if(!model.put()) {
                                    return false;
                                }
                            }, cancel: function() {}
                        }).show();
                        $('input,select,textarea').on('change', function(e) {
                            model.set_data($(e.target).attr('name'), $(e.target).val());
                        });
                    },
                    resetPwd: function(e) {
                        e.preventDefault();
                        var model = this;
                        dialog({
                            content: '将此账号密码重置为1234567,是否继续？',
                            ok: function() {
                                $.ajax({
                                    url: _global.url.api + 'partner/reset_password',
                                    type: 'put',
                                    data: {id: model.get_data('id')},
                                    success: function(res) {
                                        if(res.code == 0) {
                                            Alert({msg: '密码重置成功'});
                                        } else {
                                            Alert({msg: res.msg, type: 'error'});
                                        }
                                    }, error: function() {
                                        Alert({msg: '通讯失败', type: 'error'});
                                    }
                                });
                            },cancel: function() {}
                        }).show(e.target);
                    }
                },
                callback: function() {
                    loaded();
                }
            });
            this.collection.fetch();
        },

        event: function() {
            this.data = {};
            var _this = this;
            //保存
            $('#btn-add').on('click', function(e) {
                _this.data = {};
                e.preventDefault();
                _this.dialog = dialog({
                    title : '增加',
                    content: template('add-tpl', {}),
                    width: 600,
                    ok: function() {
                        var model = new Model({
                            url:_global.url.api + 'partner',
                            data: _this.data
                        });
                        if(_this.add_valid.isFail()) {
                            return false;
                        }
                        if(model.post()) {
                            _this.collection.reload();
                        } else {
                            return false;
                        }
                    },cancel: function() {}
                });
                _this.dialog.show();

                _this.add_valid = new Validation({
                    rules: {
                        'name': {required: true},
                        'mobile1': {required: true, minLength: 11, maxLength: 11},
                        'mobile2': {required: true, minLength: 11, maxLength: 11},
                        'email': {required: true, email: true},
                        'password': {required: true, minLength: 6, maxLength: 32}
                    }
                });

                $('input,select,textarea').on('change', function(e) {
                    _this.change(e);
                });
            });
        },

        change: function(e) {
            this.data[$(e.target).attr('name')] = $(e.target).val();
        }
    };

    new index();

});